package coml.jiyun.hp.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.Nullable;

public class MySql extends SQLiteOpenHelper {
    public MySql(@Nullable Context context) {
        super(context, "my.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table demo(name text,age text)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public  void insert(Bean bean){

        ContentValues values = new ContentValues();
        values.put("name",bean.getName());
        values.put("age",bean.getAge());
        getWritableDatabase().insert("demo",null,values);

    }
    public List<Bean> query(){
        List<Bean> list = new ArrayList<>();
        Cursor demo = getReadableDatabase().query("demo", null, null, null, null, null, null);
        while (demo.moveToNext()){
            String name = demo.getString(demo.getColumnIndex("name"));
            String age = demo.getString(demo.getColumnIndex("age"));
            list.add(new Bean(name,age));
        }
        return list;
    }
    public void delete(String msg){
        getWritableDatabase().delete("demo","name=?",new String[]{msg});
    }
    public void upDate(String name,Bean bean){
        ContentValues values = new ContentValues();
        values.put("name",bean.getName());
        values.put("age",bean.getAge());
        getWritableDatabase().update("demo",values,"name = ?",new String[]{name});
    }
}
